package xyz.zhouxf.rpc.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author zhouxf
 * @date 2022/8/20
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class RpcResponse<T> implements Serializable {

    /**
     * 响应对应请求号
     */
    private String requestId;

    /**
     * 响应状态码
     */
    private Integer statusCode;

    /**
     * 响应附带信息
     */
    private String message;

    /**
     * 响应数据
     */
    private T data;

    /**
     * @param data
     * @param requestId
     * @param <T>
     * @return
     */
    public static <T> RpcResponse<T> success(T data, String requestId) {
        RpcResponse<T> response = new RpcResponse<>();
        response.setRequestId(requestId);
        response.setStatusCode(20);
        response.setData(data);
        return response;
    }

    /**
     * @param requestId
     * @param message
     * @param <T>
     * @return
     */
    public static <T> RpcResponse<T> fail(String requestId, String message) {
        RpcResponse<T> response = new RpcResponse<>();
        response.setRequestId(requestId);
        response.setStatusCode(50);
        response.setMessage(message);
        return response;
    }
}
